<?php
/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoa_Framework
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoa_Stream_Context
 */
import('Stream.Context');

/**
 * Hoa_File_Read
 */
import('File.Read.~');

/**
 * Hoa_Xml
 */
import('Xml.~');

/**
 * Hoathis_Weather_Exception
 */
importModule('Weather.Exception');

/**
 * Class Hoathis_Weather
 *
 * @author      Gregory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Gregory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.2
 * @package     Hoathis_Weather
 */
class Hoathis_Weather {

    /**
     * Constant API weather URL
     *
     * @const URL String
     */
    const URL = 'http://www.google.com/ig/api?weather=%s&hl=%s';

    /**
     * Constant Images URL
     *
     * @const URL_IMAGES String
     */
    const URL_IMAGES = 'http://www.google.com';

    /**
     * XML Reponse
     *
     * @var $reponse Array
     */
    private $reponse = null;

    /**
     * Current city
     *
     * @var $city String
     */
    public $city = null;

    /**
     * Current conditions
     *
     * @var $current_conditions Array
     */
    public $current_conditions = array();

    /**
     * Forecast conditions
     *
     * @var $forecast_conditions Array
     */
    public $forecast_conditions = array();

    /**
     * Constructor
     *
     * @param String $city_code The label of the city
     * @param String $lang the Lang of the return weather labels
     * @return String Current conditions
     */
    function __construct ( $city_code = null, $lang = 'fr' ) {

        if (is_null($city_code))
            throw new Hoathis_Weather_Exception('Argument 1: city_code could not be null', 0);

        $this->city_code = $city_code;
        $this->lang = $lang;

        Hoa_Stream_Context::getInstance('google_weather', 'http')->addOptions(array(
            'timeout' => 30
        ));

        try {

            $content = new Hoa_File_Read(
                sprintf(self::URL, urlencode($this->city_code), $this->lang),
                Hoa_File::MODE_READ,
                'google_weather'
            );
        }
        catch ( Hoa_File_Exception $e ) {

            throw new Hoathis_Weather_Exception(
                $e->getFormattedMessage(),
                $e->getCode()
            );
        }

        $this->reponse = Hoa_Xml::parse(utf8_encode($content->readAll()), null);

        if (!$this->reponse)
            throw new Hoa_Weather_Exception('Parsing error', 1);

        if (isset($xml->weather->problem_cause))
            throw new Hoa_Weather_Exception('Service weather not found', 2);

        $this->getCurrentCity();
        $this->getCurrentConditions();
        $this->getForecastConditions();

        return $this->current_conditions;
    }

    /**
     * Get the current city from the reponse
     */
    private function getCurrentCity() {

        $this->city = (string) $this->reponse['weather']['forecast_information']['city-ATTR']['data'];
    }

    /**
     * Get the current conditions from the reponse
     */
    private function getCurrentConditions() {

        $this->current_conditions['condition'] = (string) $this->reponse['weather']['current_conditions']['condition-ATTR']['data'];
        $this->current_conditions['temp_f'] = (string) $this->reponse['weather']['current_conditions']['temp_f-ATTR']['data'];
        $this->current_conditions['temp_c'] = (string) $this->reponse['weather']['current_conditions']['temp_c-ATTR']['data'];
        $this->current_conditions['humidity'] = (string) $this->reponse['weather']['current_conditions']['humidity-ATTR']['data'];
        $this->current_conditions['icon'] = (string) self::URL_IMAGES.$this->reponse['weather']['current_conditions']['icon-ATTR']['data'];
        $this->current_conditions['wind_condition'] = (string) $this->reponse['weather']['current_conditions']['wind_condition-ATTR']['data'];
    }

    /**
     * Get the forecast conditions from the reponse
     */
    private function getForecastConditions() {

        foreach($this->reponse['weather']['forecast_conditions'] as $key => $value) {

            $this->forecast_conditions[$key]['day_of_week'] = (string) $value['day_of_week-ATTR']['data'];
            $this->forecast_conditions[$key]['low'] = (string) $value['low-ATTR']['data'];
            $this->forecast_conditions[$key]['high'] = (string) $value['high-ATTR']['data'];
            $this->forecast_conditions[$key]['icon'] = (string) self::URL_IMAGES.$value['icon-ATTR']['data'];
            $this->forecast_conditions[$key]['condition'] = (string) $value['condition-ATTR']['data'];
       }
    }

    /**
     * Get the city
     *
     * @return String Current city
     */
    public function getCity() {

        return $this->city;
    }

    /**
     * Get the current weather
     *
     * @return Array Current Conditions
     */
    public function getCurrent() {

        return $this->current_conditions;
    }

    /**
     * Get the forecast weather
     *
     * @return Array Forecast Conditions
     */
    public function getForecast() {

        return $this->forecast_conditions;
    }
}